package com.yc.sole.system.repository;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.text.CharSequenceUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yc.sole.framework.mybatis.page.Paging;
import com.yc.sole.framework.mybatis.util.PageUtil;
import com.yc.sole.system.dto.BizLogDto;
import com.yc.sole.system.entity.BizLog;
import com.yc.sole.system.mapper.BizLogMapper;
import com.yc.sole.system.request.BizLogQueryReq;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * 服务接口实现
 *
 * @author yizuomin
 * @date 2024-03-20
 **/
@Slf4j
@Service
@RequiredArgsConstructor
public class BizLogRepository extends ServiceImpl<BizLogMapper, BizLog> {

    /**
     * 新增
     *
     * @param bizLogDto
     * @return RestResult
     */
    @Transactional(rollbackFor = Exception.class)
    public void saveBizLog(BizLogDto bizLogDto) {
        BizLog entity = new BizLog();
        BeanUtil.copyProperties(bizLogDto, entity);
        this.save(entity);
    }

    /**
     * 通过Id修改
     *
     * @param bizLogDto
     * @return RestResult
     */
    @Transactional(rollbackFor = Exception.class)
    public void updateBizLogById(BizLogDto bizLogDto) {
        BizLog entity = new BizLog();
        BeanUtil.copyProperties(bizLogDto, entity);
        this.updateById(entity);
    }

    /**
     * 通过id批量删除
     *
     * @param ids
     * @return RestResult
     */
    @Transactional(rollbackFor = Exception.class)
    public void deleteBizLogByIds(Collection<Long> ids) {
        this.removeBatchByIds(ids);
    }

    /**
     * 根据id获取
     *
     * @param ids
     * @return RestResult
     */
    public List<BizLogDto> selectBizLogByIds(Set<Long> ids) {
        if (CollUtil.isEmpty(ids)) {
            return Collections.emptyList();
        }
        return BeanUtil.copyToList(this.listByIds(ids), BizLogDto.class);
    }

    /**
     * 根据id获取
     *
     * @param id
     * @return RestResult
     */
    public BizLogDto selectBizLogById(Long id) {
        return BeanUtil.copyProperties(this.getById(id), BizLogDto.class);
    }

    /**
     * 分页查询
     *
     * @param page
     * @return RestResult
     */
    public Paging<BizLogDto> selectBizLogPage(Paging page, BizLogQueryReq queryReq) {
        LambdaQueryWrapper<BizLog> queryWrapper = Wrappers.<BizLog>lambdaQuery()
                .eq(Objects.nonNull(queryReq.getBizId()), BizLog::getBizId, queryReq.getBizId())
                .eq(CharSequenceUtil.isNotBlank(queryReq.getTableName()), BizLog::getTableName, queryReq.getTableName())
                .orderByDesc(BizLog::getId);
        return PageUtil.pageCopy(this.page(PageUtil.toMybatisPage(page), queryWrapper), BizLogDto.class);
    }

    /**
     * 根据表名和业务id查询
     *
     * @param tableName
     * @param bizId
     * @return
     */
    public List<BizLogDto> selectBizLogByTableNameAndBizId(String tableName, Long bizId) {
        if (CharSequenceUtil.isBlank(tableName) || Objects.isNull(bizId)) {
            return null;
        }
        LambdaQueryWrapper<BizLog> wrapper = Wrappers.<BizLog>lambdaQuery()
                .eq(BizLog::getTableName, tableName)
                .eq(BizLog::getBizId, bizId)
                .orderByDesc(BizLog::getId);
        return BeanUtil.copyToList(this.list(wrapper), BizLogDto.class);
    }
}
